//
//  GAOWelcomeController.swift
//  Weibo28
//
//  Created by 高辰 on 16/5/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class GAOWelcomeController: UIViewController {

    //  MARK: 懒加载控件
    //背景图片
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像
    private lazy var headImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        
        if let url = GAOUserAccountViewModel.UserAccountViewModel.userAccount?.avatar_large {
            imageView.sd_setImageWithURL(NSURL(string: url), placeholderImage:UIImage(named: "avatar_default_big"))
        }
        
        return imageView
    }()
    //欢迎信息
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        //欢迎信息前加入用户名
        if let name = GAOUserAccountViewModel.UserAccountViewModel.userAccount?.name {
            label.text = name + ",欢迎回来!"
        } else {
            label.text = "欢迎回来"
        }
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        return label
        
    }()
    
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        // Do any additional setup after loading the view.
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
            startAnimation()
    }
    
    //添加子视图约束
    private func setupUI() {
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        //设置约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.centerX.equalTo(headImageView)
        }
        
    }
    
    
    private func startAnimation() {
        //改变约束
        
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        messageLabel.alpha = 0
        
        //usingSpringWithDamping  阻尼   0 - 1   阻尼越大弹性效果越小
        //initialSpringVelocity 初始速度
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 50, options: [], animations: { () -> Void in
            //让子视图重新布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
               
                UIView.animateWithDuration(1.5, animations: { () -> Void in
                    //显示messageLabel
                    self.messageLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        //  进入到首页
                        
                      NSNotificationCenter.defaultCenter().postNotificationName(SwichRootVCNotification, object: nil)
                        
                })
                
        }
        
        
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
